<?php
function upload($fileName,$uploadDir,$option = null, $prefix = 'images_'){
      $upload = new Zend_File_Transfer_Adapter_Http();      
      $upload->setDestination($uploadDir,$fileName);
      $fileInfo = $upload->getFileInfo();

      if($option == null){
         $newFileName = $fileInfo[$fileName]['name'];
         $upload->receive($fileName);
      }
      
      if($option['task'] == 'rename'){
         $newFileName = $fileInfo[$fileName]['name'];
         preg_match("/\.([^\.]+)$/", $newFileName, $matches);
         $fileExtension = $matches[1];
         //Tao ten tap tin moi
         do{
            //kiem tra su ton tai cua file, neu chua hop le thi van thuc hien            
            $newFileName = $prefix . time() .  '.' . $fileExtension;
            $countFix++;
         }while(file_exists($uploadDir . $newFileName));
         //upload file
         $upload->addFilter('Rename', array('target'=> $uploadDir . $newFileName,
                             'overwrite' => true));
         $upload->receive($fileName);
         $newFileName = $fileInfo[$fileName]['name'];
      }
       return $newFileName;
   }